﻿@using KPIS.GERP.Models.ICM


@{
    ViewBag.Title = "รายละเอียดพัสดุ [RC]";

    var service = new KPIS.GERP.WEB.GIMS.ReceiveParcelService.ReceiveParcelServiceClient();
    
    AssetWarranty modelAssetWarranty = new AssetWarranty();

    int intResTypeId = Convert.ToInt32(ViewBag.ResTypeId);
    int intRecDtlId = Convert.ToInt32(ViewBag.RecDtlId);


    int intAssetId = Convert.ToInt32(ViewBag.AssetId);

    var modelReceiveDtl = service.SelectReceiveDtl(new KPIS.GERP.Models.ICM.ReceiveDtl { ReceiveDtlId = intRecDtlId, RecordStatus = "!D" }, new[] { "MasResourceItem", "Received", "MasResourceType", "MasUnit", "MasDepreciationType", "ProjectExpense.Project" }).FirstOrDefault();
    var modelAsset = service.SelectAsset(new KPIS.GERP.Models.ICM.Asset { ReceiveDtlId = intRecDtlId, RecordStatus = "!D" },null).ToList();
    var modelWarranty = service.SelectAssetWarranty(new KPIS.GERP.Models.ICM.AssetWarranty { AssetId = intAssetId, RecordStatus = "!D" },new[] {"Asset"}).ToList();
    
    
}

@Html.Hidden("hdDtlId",intRecDtlId)
@Html.Hidden("hdResTypeId",intResTypeId)
@Html.Hidden("hdAssetId",intAssetId)
@Html.Hidden("hdNo")
@Html.Hidden("hdSelRow")
<br />

    <div class="span11 row-fluid" style="font-size:medium">
    <div class="span11">
        <b>รหัสการรับพัสดุเข้าคลัง : </b>
        <a href="@Url.Action("Details",
                     new { id = modelReceiveDtl == null ? 0 : modelReceiveDtl.ReceiveId })" > 
            @Html.DisplayFor(item => modelReceiveDtl.Received.ReceiveCode)
        </a>
    </div> 
</div>
<hr/>

<div class="span11 row-fluid" style="font-size:medium">
        <div class="span4">
            <b>ชื่อพัสดุ : </b>@Html.DisplayFor(item => modelReceiveDtl.MasResourceItem.Name)
        </div>
        <div class="span3">
            <b>ประเภทพัสดุ : </b>@Html.DisplayFor(item => modelReceiveDtl.MasResourceType.Name)
        </div>
        <div class="span3">
            <b>ชื่อโครงการ : </b>@Html.DisplayFor(item => modelReceiveDtl.ProjectExpense.Project.Name)
        </div>
    </div>

    <div class="span11 row-fluid" style="font-size:medium">
        <div class="span4">
            <b>จำนวนที่รับ : </b>@Html.DisplayFor(item => modelReceiveDtl.Quantity) 
        </div>
        <div class="span3">
            <b>หน่วย : </b>@Html.DisplayFor(item => modelReceiveDtl.MasUnit.Name) 
        </div>
        <div class="span3">
            <b>ราคาต่อหน่วย : </b>@Html.DisplayFor(item => modelReceiveDtl.Price)
        </div>
    </div>

<div class="span11 row-fluid" style="font-size:medium">
    <div class="span4">
        <b>@Html.DisplayNameFor(item => modelReceiveDtl.Remark) : </b>@Html.DisplayFor(item => modelReceiveDtl.Remark)
    </div>
    <div class="span8">
        <b>@Html.DisplayNameFor(item => modelReceiveDtl.Description) : </b>@Html.DisplayFor(item => modelReceiveDtl.Description)
    </div>
</div> 
    <div class="span11 row-fluid" style="font-size:medium">
        <div class="span8">
            @{
                string code = "";
                var db = new GERPContext();
                var dni = db.DeliveryNoteItems.Include("DeliveryNote")
                    .FirstOrDefault(o => o.DnItemId == modelReceiveDtl.DeliveryNoteItemId);
                int? dnId = null;
                if (dni != null)
                {
                    code = dni.DeliveryNote.Code;
                    dnId = dni.DeliveryNote.DnId;
                }
            }

            <div class="span11 row-fluid" style="font-size:medium">
                <div class="span11">
                    <b>จากตรวจรับ : </b>
                    <a href="@Url.Action("Details", "DeliveryNote", new { dnId })" target="_blank">@(code)</a>
                </div>
            </div>
            
            
        </div>
    </div>
<br /><br /><br /><br />
<hr />
<br />
<a href="@Url.Action("Details","ReceiveParcel")/@ViewBag.RecId" role="button"
            class="btn btn-gray button-standard-width btn-small" data-dialog-id = "aboutDialog" data-dialog-title = "แก้ไข">
    <i class="icon-reply bigger-120"></i>กลับไปหน้ารายละเอียดรายการขอเบิก
</a>

<br /><br />
<div id="dvAsset" style="display:none">
<div class="row-fluid">
    <div class="table-header span12">
        <div class="span9">
            <h4 id="Header" class="smaller">รายการครุภัณฑ์</h4>
        </div>
    </div>
</div>

<table id="tblAsset" class="table table-bordered table-hover">
    <thead>
        <tr>            
            <th style="display:none"></th>
            <th></th>
            <th style="text-align: center">หมายเลขครุภัณฑ์</th>
            <th style="text-align: center">รายการครุภัณฑ์</th>
            <th style="text-align: center">หมายเลขซีเรียล</th>
            <th style="text-align: center">วันที่ครุภัณฑ์</th>
        </tr>
    </thead>

    <tbody>
        @foreach(var m in modelAsset)
        {
        <tr>
            <td style="display:none">@Html.DisplayFor(AssetItem => m.AssetId)</td>
            <td style="text-align: center">
                @if (@m.AssetId.ToString() == ViewBag.AssetId)
                {
                     <i class="icon-check"></i>
                }
                else
                {}

            </td>            
            <td style="text-align: center">@Html.DisplayFor(AssetItem => m.AssetCode)</td>
            <td style="text-align: center">@Html.DisplayFor(AssetItem => m.AssetName)</td>
            <td style="text-align: center">@Html.DisplayFor(AssetItem => m.SerialNumber)</td>
            <td style="text-align: center">@Html.DisplayFor(AssetItem => m.AssetDate)</td>
        </tr>
        }
    </tbody>
</table>
</div>

<br />

<div id="dvWarranty" class="widget-box row-fluid">
    <button class="btn btn-mini button-standard-width btn-success " type="button" id="btnWarrantyDtl" >
        <i class="icon-plus"></i>เพิ่มประกันภัย
    </button>
    <div class="widget-header">
        <h4 class="smaller" id="header" >ประกันภัย</h4>
    </div>
    <div class="widget-body">
        <div class="widget-main">
            <div class="row-fluid">
                <table id="tblAssetWarranty"  class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th style="text-align: center">หมายเลขครุภัณฑ์</th>
                            <th style="text-align: center">เลขที่กรมธรรม์</th>
                            <th style="text-align: center">ค่าเบี้ยประกัน</th>
                            <th style="text-align: center">ระยะเวลาประกัน(เดือน)</th>
                            <th style="text-align: center">วันที่เริ่มกรมธรรม์</th>
                            <th style="text-align: center">วันที่สิ้นสุดกรมธรรม์</th>
                            <th></th>
                        </tr>
                    </thead>

                    <tbody>
                        @foreach (var m in modelWarranty)
                        {
                        <tr>
                            <td style="text-align: center">@Html.DisplayFor(WarrantyItem => m.Asset.AssetCode)</td>
                            <td style="text-align: center">@Html.DisplayFor(WarrantyItem => m.WarrantyNo)</td>
                            <td style="text-align: right">@Html.DisplayFor(WarrantyItem => m.WarrantyPrice)</td>
                            <td style="text-align: center">@Html.DisplayFor(WarrantyItem => m.WarrantyTime)</td>
                            <td style="text-align: center">@Html.DisplayFor(WarrantyItem => m.WarrantyStartDate)</td>
                            <td style="text-align: center">@Html.DisplayFor(WarrantyItem => m.WarrantyExpireDate)</td>
                            <td style="text-align: center">
                                @Html.ButtonDelete(new { id = m.WarrantyId, intAssetId = m.AssetId, intDtlId = ViewBag.RecDtlId }, true, "DeleteWarranty")
                            </td>
                        </tr>
                        }                         
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div id="mdlWarrantyModal" class="modal hide fade" tabindex="-1" role="dialog" 
    aria-labelledby="myModalLabel" aria-hidden="true" style="display:none;">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="warrantyModalLabel">เพิ่มประกันภัย</h3>
    </div>    
    <div class="modal-body">
        <div class="control-group">
            <div class="control-label">
                เลขที่กรมธรรม์ :
            </div>
            <div class="controls">
                @Html.TextBoxFor(model => modelAssetWarranty.WarrantyNo, new { style = "width: 150px;", id = "txtWarrantyNo" })
            </div>
        </div>

        <div class="control-group">
            <div class="control-label">
                ค่าเบี้ยประกัน :
            </div>
            <div class="controls">
                @Html.TextBoxFor(model => modelAssetWarranty.WarrantyPrice, new { style = "width: 150px;", id = "txtWarrantyPrice" })
            </div>
        </div>

        <div class="control-group">
            <div class="control-label">
                ระยะเวลาประกัน :&nbsp
            </div>
            <div class="controls">
                @Html.TextBoxFor(model => modelAssetWarranty.WarrantyTime, new { style = "width: 150px;", id = "txtWarrantyTime" })&nbsp;เดือน
            </div>
        </div>

        <div class="control-group">
            <div class="control-label">
                วันที่เริ่มกรมธรรม์ :
            </div>
            <div class="controls">
                <div class="row-fluid input-append date">  
                    @Html.TextBoxFor(model => modelAssetWarranty.WarrantyStartDate, new { style = "width: 150px;", id = "WarrantyStartInsurance", @class = "date-picker" }) 
                </div>
            </div>
        </div>

        <div class="control-group">
            <div class="control-label">
                วันที่สิ้นสุดกรมธรรม์ :&nbsp
            </div>
            <div class="controls">
                 @Html.TextBoxFor(model => modelAssetWarranty.WarrantyExpireDate, new { style = "width: 150px;", id = "WarrantyEndInsurance", @class = "date-picker" })
            </div>
        </div>
        
    </div>
    <div class="modal-footer">
        <button type="button" id="btnAddWarranty">บันทึกข้อมูล</button>
    </div>

</div>


@section scripts {

    @*<link href="~/Content/Ace/assets/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/Ace/assets/js/bootbox.min.js"></script>*@

    <script type="text/javascript">

        $('.date-picker').datepicker({ language: 'th-th', format: 'dd/mm/yyyy' });
        
        function FetchWarrantyTable(AssetId)
        {
            var DtlId = $("#hdDtlId").val();
            var a = ('@Url.Action("SubDetails","ReceiveParcel")'+"?id=" + DtlId + "&intAssetId=" + AssetId);
            window.location.href = a;

            var Id = $("#hdAssetId").val();
            if (Id == AssetId) {
                $('#tblAsset tr').find('td:eq(0)').text(AssetId).css('background-color', '#99CCCC');
            }
            else {
                $('#tblAsset tr').css('background-color', 'white');                
            }


            $("#dvAsset").show();
            $("#dvWarranty").show();

            
        }
        
        // show/hide AssetWarranty
        jQuery(function ($) {
            var AssetId = $("#hdAssetId").val()
            if (AssetId == "0") {
                $("#dvWarranty").hide();
            }
            else {
                $("#dvWarranty").show();
            }
        });
        
        // แสดงตาราง AssetWarranty
        jQuery(function ($) {
            var ResourceType = $("#hdResTypeId").val()

            // กรณีพัสดุเป็นครุภัณฑ์
            if (ResourceType == 2) {
                $("#dvAsset").show();

                // Get AssetId from TableAsset
                $('#tblAsset').find('tr').click(function () {
                    //display Waranty Table
                    $("#dvWarranty").show();

                    // GetAssetId when select row
                    var AssetId = $(this).closest('tr').find('td:eq(0)').text();

                    // แสดงตาราง AssetWarranty ตามที่ select
                    FetchWarrantyTable(AssetId);

                    // height light row
                    $('#tblAsset tr').css('background-color', 'white');
                    $(this).css('background-color', '#99CCCC');
                });
            }
            else {
                $("#dvAsset").hide();
                $("#dvWarranty").hide();
            }
            // CreateWarranty ครุภัณฑ์แต่ละชิ้น
            AddWarranty($("#hdAssetId").val());            
        })

        // แสดง popup สร้าง AssetWarranty        jQuery(function ($) {
            $("#btnWarrantyDtl").click(function () {
                $("#mdlWarrantyModal").modal('show');
            })
        })

        // Validate WarrantyPrice
        $("#txtWarrantyPrice").bind('keypress', function (e) {
            return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && (e.which != 46 || $(this).val().indexOf('.') != -1)) ? false : true;
        })

        // Validate WarrantyTime
        $("#txtWarrantyTime").bind('keypress', function (e) {
            return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) ? false : true;
        })

        // ฟังก์ชั่น Create AssetWarranty        function AddWarranty(itemId) {
            $("#btnAddWarranty").click(function () {
                bootbox.confirm("ต้องการบันทึก ใช่หรือไม่?", function (result) {
                    if (result) {
                        $.ajax({
                                url: '@Url.Action("CreateWarranty", "ReceiveParcel")',
                                type: 'POST',
                                data: {
                                    strNo: $("#txtWarrantyNo").val(),
                                    decPrice: $("#txtWarrantyPrice").val(),
                                    intTime: $("#txtWarrantyTime").val(),
                                    strStart: $("#WarrantyStartInsurance").val(),
                                    strEnd: $("#WarrantyEndInsurance").val(),
                                    intType: $("#hdResTypeId").val(),
                                    intId: itemId,
                                    intRecDtlId: $("#hdDtlId").val()
                                },
                                //dataType: "json",
                                success: function (result) {
                                    $("#mdlWarrantyModal").modal('hide');
                                    FetchWarrantyTable($("#hdAssetId").val());
                                },
                                error: function () {
                                    alert("กรุณากรอกข้อมูลให้ครบถ้วน");
                                }
                        });
                    }
                });
                return false;
           });
        }


    </script>
}